using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using TTDotNet.Engine.Vehicles;

namespace TTDotNet.Engine.Files
{
    public class RoadVehicleLoader : FileLoader
    {

        public RoadVehicleLoader()
            : base("../../data/vehicles/road_vehicles.xml")
        {

        }

        public List<VehicleInfo> GetVehicleInfo()
        {
            List<VehicleInfo> vehicleInfos = new List<VehicleInfo>();

            try
            {
                reader.ReadStartElement("Vehicles");

                while (reader.IsStartElement())
                {
                    string name = reader.GetAttribute("Name");
                    DateTime yearInvented = new DateTime(Int32.Parse(reader.GetAttribute("YearInvented")), 1, 1);
                    int maxSpeed = Int32.Parse(reader.GetAttribute("MaxSpeed"));
                    decimal baseCost = Decimal.Parse(reader.GetAttribute("BaseCost"), System.Globalization.NumberStyles.Currency);
                    decimal baseMCost = Decimal.Parse(reader.GetAttribute("MaintenanceCost"), System.Globalization.NumberStyles.Currency);
                    int lifeLength = Int32.Parse(reader.GetAttribute("LifeLength"));
                    int modelLength = Int32.Parse(reader.GetAttribute("ModelLife"));

                    reader.ReadStartElement("Vehicle");

                    reader.ReadStartElement("Carries");

                    List<CargoInfo> cargoCapacities = new List<CargoInfo>();

                    VehicleType vType = VehicleType.Lorry;

                    while(reader.Name.Equals("Cargo")) 
                    {
                        CargoType cargo = (CargoType)Enum.Parse(typeof(CargoType), reader.GetAttribute("Name"));
                        if (cargo == CargoType.Passengers) vType = VehicleType.Bus;
                        int capacity = Int32.Parse(reader.GetAttribute("Capacity"));
                        cargoCapacities.Add(new CargoInfo(cargo, capacity));
                        reader.ReadStartElement("Cargo");
                    }

                    reader.ReadEndElement();

                    reader.ReadStartElement("Meshes");

                    List<string> meshPaths = new List<string>();

                    while(reader.Name.Equals("Mesh")) 
                    {
                        meshPaths.Add(reader.GetAttribute("Path"));
                        reader.ReadStartElement("Mesh");
                    }

                    reader.ReadEndElement();
                    reader.ReadEndElement();

                    VehicleInfo vInfo = new VehicleInfo(vType, name, yearInvented, maxSpeed, 1, baseCost, baseMCost, cargoCapacities, meshPaths, lifeLength, modelLength);

                    vehicleInfos.Add(vInfo);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error parsing XML, " + e.Message);
                Environment.Exit(1);
                return null;
            }

            return vehicleInfos;

        }
    }
}
